package org.hgvc.webapp.ets.jsf.beans.mastersetup;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.component.UIParameter;
import javax.faces.event.ActionEvent;

import org.hgvc.webapp.ets.bo.Agent;
import org.hgvc.webapp.ets.bo.HotelResort;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hgvc.webapp.ets.spring.service.MasterSetupService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;


public class HotelResortBean extends AbstractBean {
	public static final String BEAN_NAME="hotelResortBean";
	private MasterSetupService service;
	private List<HotelResort> hotelResortList;
	private HotelResort hotelResort;
	public static final String NAV_LIST_HOTELRESORT="listHotelResort";
	public static final String NAV_ADD_HOTELRESORT="addHotelResort";
	public static final String NAV_EDIT_HOTELRESORT="editHotelResort";
	 
	public MasterSetupService getService() {
		return service;
	}
	public void setService(MasterSetupService service) {
		this.service = service;
	}
	public HotelResort getHotelResort() {
		return hotelResort;
	}
	public void setHotelResort(HotelResort hotelResort) {
		this.hotelResort = hotelResort;
	}
	public List<HotelResort> getHotelResortList() {
		try{
			hotelResortList = service.getHotelResortList();
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return hotelResortList;
	}
	public void setHotelResortList(List<HotelResort> hotelResortList) {
		this.hotelResortList = hotelResortList;
	}
	
	public String navCreationAction(){
		hotelResort = new HotelResort();
		return NAV_ADD_HOTELRESORT;
	}
	
	public String createAction(){
		try{
			hotelResort.setCreatedBy(getThisUser());
			hotelResort.setCreatedDate(new Date());
			hotelResort.setIsDeleted(CommonConstant.NOT_DELETED);
			service.saveHotelResort(hotelResort);
			return NAV_LIST_HOTELRESORT;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_ADD_HOTELRESORT;
	}
	
	public void editListener(ActionEvent actionEvent){
		hotelResort = null;
		UIParameter parameter = (UIParameter)actionEvent.getComponent().findComponent("hotelResortId");
		String hotelResortId = (String)parameter.getValue();
		for(HotelResort h:hotelResortList){
			if(h.getHotelResortId().equalsIgnoreCase(hotelResortId)){
				hotelResort = h;
				break;
			}
		}
	}
	
	public String navEditAction(){
		return NAV_EDIT_HOTELRESORT;
	}
	
	public String editAction(){
		try{
			hotelResort.setModifiedBy(getThisUser());
			hotelResort.setModifiedDate(new Date());
			service.updateHotelResort(hotelResort);
			return NAV_LIST_HOTELRESORT;
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		
		return NAV_EDIT_HOTELRESORT;
	}
	
	public String deleteAction(){
		try{
			
			List<HotelResort> deleteList = new ArrayList<HotelResort>();
			for(HotelResort h:hotelResortList){
				if(h.isMarkAsAction()){
					deleteList.add(h);
				}
			}
			service.deleteHotelResorts(deleteList);
		}catch(BaseException ex){
			ex.printStackTrace();
			initErrorBean(ex);
		}
		return NAV_LIST_HOTELRESORT;
	}
}
